﻿Imports MySoft.Map.Data
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class ProceedPlanAdjustSP_Business
    Inherits Mysoft.Map.Application.Security.AppPage

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        Dim dtTemp As DataTable
        Dim dtTempDtl As DataTable
        Dim dtTempDoc As DataTable
        Dim iDocCount As Integer
        Dim strSQL1, strSQL2, strSQL3 As String
        Dim xmlDoc As New XmlDocument
        Dim rootNode As XmlNode
        Dim AdjustAmount As Decimal = 0

        Dim i, j As Integer
        Dim xslt As New Xsl.XslTransform

        strSQL1 = "SELECT DeptAdjustGUID,Subject,DeptGUID,Year,vys_myBusinessUnit.BUName,Adjuster,AdjustDate,AdjustReason,ApproveState,ys_DeptAdjust.Remarks " & _
                 " FROM ys_DeptAdjust left join vys_myBusinessUnit on ys_DeptAdjust.deptguid=vys_myBusinessUnit.buguid " & _
                  " WHERE DeptAdjustGUID = '" & Request.QueryString("oid") & "'"
        '预算调整单据表信息
        dtTemp = MyDB.GetDataTable(strSQL1)


        strSQL2 = " SELECT a.DeptAdjustDtlGUID,a.DeptAdjustGUID,a.ProceedingGUID,a.CostGUID,a.Year,cast(a.Year as nvarchar(40)) + '-' + case when len(cast(a.month as nvarchar(40)))<>2 then '0' + cast(a.month as nvarchar(40)) else cast(a.month as nvarchar(40)) end Month,c.costshortname,case when d.ProceedingName is null then '日常费用' else d.ProceedingName end AS ProceedingName,a.AdjustAmount,a.Remarks,b.DeptGUID" & _
                  " FROM ys_DeptAdjustDtl a " & _
                  " LEFT JOIN ys_DeptAdjust b ON a.DeptAdjustGUID=b.DeptAdjustGUID " & _
                  " LEFT JOIN ys_DeptCost c on c.DeptCostGUID=a.CostGUID" & _
                  " LEFT JOIN ys_Proceeding d on d.ProceedingGUID=a.ProceedingGUID" & _
                  " WHERE a.DeptAdjustGUID = '" & Request.QueryString("oid") & "'"

        '预算调整明细表信息
        dtTempDtl = MyDB.GetDataTable(strSQL2)

        strSQL3 = "SELECT DocGUID as oid,Replace(DocName,'&','%26') as DocName, " & _
                  "CASE WHEN FileName<>'' THEN Location+Replace(FileName,'&','26%') ELSE Replace(URL,'&','26%') END AS url FROM ep_Documents  " & _
                  "WHERE FkGUID='" & Request.QueryString("oid") & "' AND DocType='部门费用调整单' ORDER BY CreateOn DESC"
        '文档信息
        dtTempDoc = MyDB.GetDataTable(strSQL3)
        iDocCount = dtTempDoc.Rows.Count

        For i = 0 To dtTempDtl.Rows().Count - 1
            If dtTempDtl.Rows(i).Item("AdjustAmount").ToString <> "" Then
                AdjustAmount += Convert.ToDecimal(dtTempDtl.Rows(i).Item("AdjustAmount").ToString)
            End If
        Next
        AdjustAmount = Format(AdjustAmount, "#,##0.00")

        xmlDoc.LoadXml("<?xml version='1.0' encoding='utf-8'?><Process></Process>")
        rootNode = xmlDoc.SelectSingleNode("/Process")
        Dim AdjustElement As XmlElement = xmlDoc.CreateElement("Adjust")

        AdjustElement.SetAttribute("subjectValue", dtTemp.Rows(0).Item("Subject").ToString)
        AdjustElement.SetAttribute("projNameValue", dtTemp.Rows(0).Item("BUName").ToString)
        AdjustElement.SetAttribute("adjusterValue", dtTemp.Rows(0).Item("Adjuster").ToString)
        AdjustElement.SetAttribute("remarksValue", dtTemp.Rows(0).Item("AdjustReason").ToString)
        AdjustElement.SetAttribute("adjustAmountValue", AdjustAmount.ToString)
        AdjustElement.SetAttribute("DocCount", iDocCount.ToString)

        For i = 0 To dtTempDtl.Rows().Count - 1
            Dim AdjustDtlElement As XmlElement = xmlDoc.CreateElement("AdjustDtl")
            AdjustDtlElement.SetAttribute("ProceedingName", dtTempDtl.Rows(i).Item("ProceedingName").ToString)
            AdjustDtlElement.SetAttribute("CostName", dtTempDtl.Rows(i).Item("costshortname").ToString)
            AdjustDtlElement.SetAttribute("Month", dtTempDtl.Rows(i).Item("Month").ToString)
            If dtTempDtl.Rows(i).Item("AdjustAmount").ToString <> "" Then
                AdjustDtlElement.SetAttribute("adjustAmountValue", Format(Convert.ToDecimal(dtTempDtl.Rows(i).Item("AdjustAmount")), "#,##0.00"))
            End If
            AdjustDtlElement.SetAttribute("remarksDtlValue", dtTempDtl.Rows(i).Item("Remarks").ToString)
            AdjustElement.AppendChild(AdjustDtlElement)
        Next

        If iDocCount <> 0 Then
            For j = 0 To iDocCount - 1
                Dim DocElement As XmlElement = xmlDoc.CreateElement("Doc")
                DocElement.SetAttribute("oid", dtTempDoc.Rows(j).Item("oid").ToString)
                DocElement.SetAttribute("DocName", dtTempDoc.Rows(j).Item("DocName").ToString)
                DocElement.SetAttribute("url", dtTempDoc.Rows(j).Item("url").ToString)

                AdjustElement.AppendChild(DocElement)
            Next
        End If

        rootNode.AppendChild(AdjustElement)

        xslt.Load(Server.MapPath("/MyWorkflow/XSLT/WF_YSProceed_Adjust.xslt"))
        Dim output As New StringBuilder("")
        Dim writer As New StringWriter(output)

        xslt.Transform(xmlDoc, Nothing, writer, Nothing)
        tdContent.InnerHtml = output.ToString()

        '判断是否有查看流程权限
        txtOpenProcessRights.Text = "0"
        If Mysoft.Map.Application.Security.User.IsAdmin(Session("UserGUID")) Then
            txtOpenProcessRights.Text = "1"
        ElseIf Not (Mysoft.Map.Application.Security.User.GetUserRights(Session("UserGUID"), "02010502")("19") Is Nothing) Then
            txtOpenProcessRights.Text = "1"
        End If
    End Sub
End Class
